package com.polaris.live.popup

import android.content.Context
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.PopupSelectVideoBinding
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils

/**
 * SelectVideoPopup     开播选择
 *
 * @author Created by 半仙 on 2023/9/25/025 14:46
 */
class SelectVideoPopup(
    context: Context,
    private val onlyWait: Boolean? = null,
    private val onSelectListener: ((type: Int) -> Unit),
) : BottomPopupView(context) {

    private lateinit var mBinding: PopupSelectVideoBinding

    override fun getImplLayoutId(): Int {
        return R.layout.popup_select_video
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupSelectVideoBinding.bind(popupImplView)
        //新用户免费机会  无免费机会

        if (CommonUtils.isMan(UserManager.getUser().sex)) {
            mBinding.llCenter.visibility = View.GONE
            if (UserManager.getUser().call?.callAsset == true) {
                mBinding.startMatch.text = StringUtils.getString(string.free_match)
            } else {
                mBinding.startMatch.text = StringUtils.getString(string.start_match)
            }
            if (UserManager.getUser().sex == AppConst.UserSex.NONE) {
                mBinding.startMatch.text = StringUtils.getString(string.free_match)
            }
        } else {
            mBinding.startMatch.text = StringUtils.getString(string.start_match)
        }
        if (!CommonUtils.isMan(UserManager.getUser().sex) && (onlyWait == null || onlyWait == false)) {
            mBinding.startMatch.visibility = View.GONE
        }
        mBinding.apply {
            startMatch.setOnSingleClickListener {
                if (CommonUtils.isMan(UserManager.getUser().sex)) {
                    onSelectListener.invoke(LiveConst.Type.CLIENT_MATCH)
                } else {
                    onSelectListener.invoke(LiveConst.Type.ONLY_MATCH)
                }
                dismiss()
            }
            matchLivestreaming.setOnSingleClickListener {
                onSelectListener.invoke(LiveConst.Type.LIVE_AND_MATCH)
                dismiss()
            }
            startLive.setOnSingleClickListener {
                onSelectListener.invoke(LiveConst.Type.ONLY_LIVE)
                dismiss()
            }
        }

        NavigationBarUtils.setNavigationHeightIfTransparent(mBinding.vLine) { view, height ->
            val layoutParams = view.layoutParams as ViewGroup.LayoutParams
            layoutParams.height = height
            mBinding.vLine.layoutParams = layoutParams
        }
    }
}